/**
 * Created by arter on 2017/7/9.
 */
const moment = require('moment');
moment.locale('zh-cn');
const path = require('path');
module.exports = {
  get isMobile() {
    return this.ctx.isMobile;
  },
  moment: moment,
  mtpl(action = this.ctx.action) {
    const c = this.ctx.controller.split('/');
    c.splice(this.ctx.controller.split('/').length - 1, 0, 'mobile');
    return (temp = `${c.join('/')}_${action}`);
  },
  para(param) {
    return this.get(param) || this.post(param);
  },
  get isweixin() {
    let flag = false;
    const agent = this.userAgent.toLowerCase();
    // let key = ["mqqbrowser","micromessenger"];
    const key = ['micromessenger'];
    // 排除 Windows 桌面系统
    if (
      !(agent.indexOf('windows nt') > -1) ||
      (agent.indexOf('windows nt') > -1 &&
        agent.indexOf('compatible; msie 9.0;') > -1)
    ) {
      // 排除苹果桌面系统
      if (
        !(agent.indexOf('windows nt') > -1) &&
        !agent.indexOf('macintosh') > -1
      ) {
        for (const item of key) {
          if (agent.indexOf(item) > -1) {
            flag = true;
            break;
          }
        }
      }
    }
    return flag;
  },
  extModel(
    modelName = '',
    extName = '',
    config = this.config('model.mysql'),
    prefix = 'ext_'
  ) {
    const p = this.ctx.controller.split('/');
    extName = think.isEmpty(extName) ? p[1] : extName;
    return think.extModel(modelName, extName, config, prefix);
  },
  extService(name = '', ser = '', ...args) {
    const p = this.ctx.controller.split('/');
    ser = think.isEmpty(ser) ? p[1] : ser;
    return think.extService(name, ser, ...args);
  },
  extDisplay(p = this.ctx.action, m = '') {
    // console.log(this.ctx.controller);
    const c = this.ctx.controller.split('/');
    if (p === 'm' || !think.isEmpty(m)) {
      if (p === 'm') p = this.ctx.action;
      const pp = path.join(
        think.ROOT_PATH,
        'src',
        'controller',
        'ext',
        c[1],
        'view',
        'mobile',
        c[2]
      );
      return this.display(`${pp}_${p}`);
    } else {
      const pp = path.join(
        think.ROOT_PATH,
        'src',
        'controller',
        'ext',
        c[1],
        'view',
        'pc',
        c[2]
      );
      return this.display(`${pp}_${p}`);
    }
  },
  modService(name = '', ser = '', ...args) {
    let p = this.ctx.controller.split('/');
    if (
      this.ctx.controller === 'cmswing/route' ||
      this.ctx.controller === 'cmswing/modadminbase'
    ) {
      p = `mod/${this.mod.name}/index`.split('/');
    }
    ser = think.isEmpty(ser) ? p[1] : ser;
    return think.modService(name, ser, ...args);
  },
};
